<?php
declare (strict_types = 1);

namespace app\model;

use app\common\Amap;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
use think\facade\Env;
use think\Model;

/**
 * @property array $data_json 价格信息
 * @property bool $origin 信息来源:1=采集,2=后台上传
 * @property bool $property 物业类型:1=住宅用房,2=工业用房,3=商业用房,4=其他用房
 * @property bool $stage 拍卖阶段:1=一拍,2=二拍,3=重新拍卖,4=变卖
 * @property bool $status 状态:1=正在进行,2=即将开始,3=已结束,4=中止,5=撤回
 * @property bool $type 类型:1=司法拍卖,2=资产拍卖
 * @property float $nowprice 当前价格
 * @property float $startprice 起拍价
 * @property int $bid_count 出价次数
 * @property int $deletetime
 * @property int $district_id 所在区
 * @property int $endtime 结束时间
 * @property int $enroll_count 报名人数
 * @property int $id
 * @property int $starttime 开始时间
 * @property int $updatetime
 * @property int $watch_count 围观人数
 * @property string $createtime
 * @property string $design 几室几厅
 * @property string $files 附件
 * @property string $house 小区名称
 * @property string $images 图片
 * @property string $introdu 标的物介绍
 * @property string $know 竞买须知
 * @property string $location 位置信息(经纬度 逗号分隔)
 * @property string $name 房产名称
 * @property string $notice 竞买公告
 * @property string $space 占地面积
 * @property string $trade_log 竞拍记录
 * @property string $uniqid 保留字段  唯一ID
 * @property string $videos 视频
 * @property-read \app\model\UserCollect $user_collect
 * @property-read mixed $images_text
 * @property-read mixed $origin_text
 * @property-read mixed $property_text
 * @property-read mixed $status_text
 * @property-read mixed $type_text
 * @mixin \think\Model
 */
class Room extends Model
{
	protected $createTime = 'createtime';
	
	protected $append = [
		'status_text',
		'property_text',
		'type_text',
		'origin_text',
		'images_text',
		'endtime_text',
		'starttime_text',
	
	];
	
	protected $type = [
		'data_json'=>'json',
		'trade_log'=>'json',
	];
	
	public static function onAfterRead(Room $model)
	{
		$amap = Amap::getInstance();
		if (empty($model->house)&&!empty($model->location)){
		
			$poitype = '120000|120100|120200|120300';

			$ret = json_decode($amap->regeo($model->location,$poitype));
			
			if ($ret->status==1){
				$house = $ret->regeocode->aois;
				if (!empty($house)){
					$model->house = $house[0]->name;
					$model->save();
				}
				
			}
		}
	}
	
	function getImagesTextAttr($value,$data)
	{
		$value = $value ?? ($data['images'] ?? '');
		if ($data['origin']==2){

			if (!empty($value)){
	
				$admin_url = env('ADMIN_URL');
	
				$images = explode(',', $value);
				foreach ($images as &$image){
					$image = $admin_url.$image;
				}
				return $images;
			}else{
				return [];
			}
		}else{
			return $value?explode(',', $value):[];
		}
	}
	
	function getVideosAttr($value,$data)
	{
		$value = $value ?? ($data['video'] ?? '');
		if ($data['origin']==2&&!empty($value)){
				$admin_url = env('ADMIN_URL');
				return  $admin_url.$value;
		}
	}
	
	
	public function userCollect()
	{
		return $this->hasOne(UserCollect::class,'room_id');
	}
	


	
	public function getStatusTextAttr($value, $data)
	{
		$value = $value ? $value : ($data['status'] ?? '');
		$list = ['1' => '正在进行', '2' => '即将开始', '3' => '已结束', '4' => '中止', '5' => '撤回'];
		return $list[$value] ?? '';
	}
	
	public function getPropertyTextAttr($value, $data)
	{
		$value = $value ? $value : ($data['property'] ?? '');
		$list = ['1' => '住宅用房', '2' => '工业用房', '3' => '商业用房', '4' => '其他用房'];
		return $list[$value] ?? '';
	}
	
	public function getEndtimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] / 1000: '');
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	public function getStarttimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] / 1000: '');
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	public function getTypeTextAttr($value, $data)
	{
		$value = $value ? $value : ($data['type'] ?? '');
		$list = ['1' => '司法拍卖', '2' => '资产拍卖'];
		return $list[$value] ?? '';
	}
	
	public function getOriginTextAttr($value, $data)
	{
		$value = $value ? $value : ($data['origin'] ?? '');
		$list = ['1' => '采集', '2' => '后台上传'];
		return $list[$value] ?? '';
	}
}
